package com.sugar.follow.up.module.business.service.followuptemplatequestion;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.sugar.follow.up.module.business.controller.admin.followuptemplatequestion.vo.*;
import com.sugar.follow.up.module.business.dal.dataobject.followuptemplatequestion.FollowUpTemplateQuestionDO;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;

import com.sugar.follow.up.module.business.dal.mysql.followuptemplatequestion.FollowUpTemplateQuestionMapper;

import static com.sugar.follow.up.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.follow.up.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.follow.up.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.follow.up.module.business.enums.ErrorCodeConstants.*;

/**
 * 随访模板题目 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class FollowUpTemplateQuestionServiceImpl implements FollowUpTemplateQuestionService {

    @Resource
    private FollowUpTemplateQuestionMapper followUpTemplateQuestionMapper;

    @Override
    public Long createFollowUpTemplateQuestion(FollowUpTemplateQuestionSaveReqVO createReqVO) {
        if (createReqVO.getType().equals(2))
            createReqVO.setOptions(null);

        // 插入
        FollowUpTemplateQuestionDO followUpTemplateQuestion = BeanUtils.toBean(createReqVO, FollowUpTemplateQuestionDO.class);
        followUpTemplateQuestionMapper.insert(followUpTemplateQuestion);

        // 返回
        return followUpTemplateQuestion.getId();
    }

    @Override
    public void updateFollowUpTemplateQuestion(FollowUpTemplateQuestionSaveReqVO updateReqVO) {
        // 校验存在
        validateFollowUpTemplateQuestionExists(updateReqVO.getId());
        if (updateReqVO.getType().equals(2))
            updateReqVO.setOptions(null);

        // 更新
        FollowUpTemplateQuestionDO updateObj = BeanUtils.toBean(updateReqVO, FollowUpTemplateQuestionDO.class);
        followUpTemplateQuestionMapper.updateById(updateObj);
    }

    @Override
    public void deleteFollowUpTemplateQuestion(Long id) {
        // 校验存在
        validateFollowUpTemplateQuestionExists(id);
        // 删除
        followUpTemplateQuestionMapper.deleteById(id);
    }

    @Override
        public void deleteFollowUpTemplateQuestionListByIds(List<Long> ids) {
        // 删除
        followUpTemplateQuestionMapper.deleteByIds(ids);
        }


    private void validateFollowUpTemplateQuestionExists(Long id) {
        if (followUpTemplateQuestionMapper.selectById(id) == null) {
            throw exception(FOLLOW_UP_TEMPLATE_QUESTION_NOT_EXISTS);
        }
    }

    @Override
    public FollowUpTemplateQuestionDO getFollowUpTemplateQuestion(Long id) {
        return followUpTemplateQuestionMapper.selectById(id);
    }

    @Override
    public PageResult<FollowUpTemplateQuestionDO> getFollowUpTemplateQuestionPage(FollowUpTemplateQuestionPageReqVO pageReqVO) {
        return followUpTemplateQuestionMapper.selectPage(pageReqVO);
    }

}